﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using SMEsCustomer360Degree.Models;
using SMEsCustomer360Degree.Models.ViewModels.Account;
using Common.Service;
using Common.Util;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Record;
using Common.Type;
using SMEsCustomer360Degree.Core.Configuration;
using ldapif;

namespace SMEsCustomer360Degree.Controllers
{
    [Authorize]
    //[InitializeSimpleMembership]
    public class AccountController : ControllerBase
    {


        #region LogOut
        [HttpGet]
        public ActionResult LogOut()
        {
            if (HttpContext != null && HttpContext.Session != null) HttpContext.Session.Clear();
            FormsAuthentication.SignOut();
            return RedirectToAction("login");
        }
        #endregion


        #region Login actions
        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            if (!string.IsNullOrEmpty(this.CurrentUser))
                return Redirect("~/");
            //ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        //
        // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        public ActionResult Login(LoginViewModel vm)
        {
            ModelState.Clear(); //clear old status
            try
            {
                if (HttpContext != null && HttpContext.Session != null) HttpContext.Session.Clear();
                if (ModelState.IsValid)
                {
                    vm.UserName = vm.UserName.ToUpper();
                    //string name = "uid=" + vm.UserName + ",ou=people,ou=user,dc=vpb,dc=com,dc=vn";
                    //LDAPUSER._ldapServerName = "10.36.10.101";
                    //LDAPUSER._port = 389;
                    //string result = LDAPUSER.IsAuthenticate(name, vm.Password).Trim();
                    //if (result.Equals("Thực hiện thành công"))
                    //{
                        using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
                        {
                            UserRecord request = new UserRecord { ApplicationKey = SystemConfiguration.ApplicationKey, UserName = vm.UserName, Password = vm.Password };
                            var response = serviceClient.Proxy.CheckLogin(request);
                            if (response.IsSuccess)
                            {
                                // Save cookie when Login success
                                FormsAuthentication.SetAuthCookie(vm.UserName, vm.IsRemember);
                                return RedirectToAction("TotalCustomer", "CustomerManager", new { area = string.Empty });
                            }
                        }
                    //}
                    //else
                    //{
                    //    vm.MessageType = MessageType.Error;
                    //    vm.Message = result;
                    //    return View(vm);
                    //}

                }
                //string Current user = User.Identity.Name;
                // If we got this far, something failed, redisplay form 
                vm.MessageType = MessageType.Error;
                vm.Message = @"The user name or password provided is incorrect.";
                return View(vm);
            }
            catch (Exception ex)
            {
                vm.MessageType = MessageType.Error;
                vm.Message = ex.ToString();
                return View(vm);
            }
        }
        #endregion

    }
}
